home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / comms / other / getit / core / getitfun.rexx < prev   
OS/2 REXX Batch file  |  1999-06-14  |  11KB  |  324 lines

  1. /* */
  2.  
  3. signal on break_c
  4. signal on halt
  5. signal on syntax
  6.  
  7. ADDRESS VALUE word(arg(1),1)
  8. "MACRO" x2d(pragma(ID))
  9. call readArguments
  10.  
  11. call getStrings
  12. call resolveHost
  13. call connectHost
  14. call sendRequest
  15. call receiveHead
  16. call openFile
  17. call SetSocketSignals(2**12)
  18. call parseLength
  19. call receive
  20. exit
  21. /**************************************************************************/
  22. receive: procedure expose global.
  23.     dummyL=200
  24.     dummyS=copies(" ",dummyL)
  25.     if global.range~="" then tl=global.range
  26.     else tl=0
  27.  
  28.     ctl=0
  29.     "CURRENT" tl
  30.  
  31.     cps=0
  32.     go=1
  33.     sel.read.0=global.sock
  34.     call time("R")
  35.     stal=0
  36.     do while go
  37.         string=""
  38.         res = WaitSelect("SEL",0,800000,2**12)
  39.         et=time("E")
  40.         if and(sel.signals,2**12)~=0 then call break_c
  41.         ets="ETime:" etime(et)
  42.         if res=0 then do
  43.             stal=stal+1
  44.             if stal>=5 then string=ets global.str.STALLED tl global.str.BYTES
  45.         end
  46.         else do
  47.             stal=0
  48.             len=recv(global.sock,"BUF",1024)
  49.             if len<0 then call err global.str.RXERR
  50.             if len>0 then do
  51.                 if WriteCH("OUT",buf)~=length(buf) then call err global.str.TXERR
  52.                 tl=tl+len
  53.                 ctl=ctl+len
  54.             end
  55.             else go=0
  56.         end
  57.         if stal<5 then do
  58.             if ctl~=0 & et~=0 then cps=(ctl/et)%1
  59.             if global.length~="" then do
  60.                 "CURRENT" tl
  61.                 string=ets global.str.SIZE":" tl || "/"global.length "(" || (tl*100)%global.length"%%)"
  62.             end
  63.             else string=ets "-" tl "("global.str.UNKNOWSIZE")"
  64.             string=string global.str.AT cps "cps"
  65.         end
  66.         if string~="" then do
  67.             string=left(string||dummyS,dummyL)
  68.             "INFO" string
  69.         end
  70.     end
  71.     et=time("E")
  72.     call CloseSocket(global.sock)
  73.     if global.length~="" then
  74.         if tl~=global.length then call err global.str.ASPECTED global.length "-" global.str.RECEIVED tl "bytes"
  75.     "END"
  76.     return
  77. /**************************************************************************/
  78. readArguments: procedure expose global.
  79.     parm.5.value=80
  80.     parm.6.value=0
  81.     parm.7.value=""
  82.     parm.8.value=8080
  83.     parm.10.value=""
  84.     parm.11.value=""
  85.     parm.12.value=""
  86.     parm.13.value=""
  87.     parm.14.value=""
  88.  
  89.     /*            0       1     2        3         4          5               6           7       8             9           10       11      12        13      14        */
  90.     TEMPLATE = "PORT/A,URL/A,HOST/A,REMOTE/A,LOCALE/A,HOSTPORT/K/N,TIMEOUT/K/N,PROXY/K,PROXYPORT/K/N,NOCACHE/S,LOGIN/K,PASS/K,RANGE/K/N,IMS/K,REFERER/K"
  91.     if ~RMH_ReadArgs(TEMPLATE) then call ERR DosString(IoErr())
  92.     global.port=parm.0.value
  93.     global.url=parm.1.value
  94.     global.host=parm.2.value
  95.     global.remote=parm.3.value
  96.     global.locale=parm.4.value
  97.     global.hostport=parm.5.value
  98.     global.timeout=parm.6.value
  99.     global.proxy=parm.7.value
  100.     global.proxyport=parm.8.value
  101.     global.nocache=parm.9.flag
  102.     global.login=parm.10.value
  103.     global.pass=parm.11.value
  104.     global.range=parm.12.value
  105.     global.ims=parm.13.value
  106.     global.referer=parm.14.value
  107.     global.cat="GetIt.catalog"
  108.     global.u="http://"
  109.     if global.login~="" then global.u=global.u||global.login":"global.pass"@"
  110.     global.u=global.u||global.host":"global.hostport"/"global.remote global.referer
  111.     return
  112. /**************************************************************************/
  113. info: procedure expose global.
  114. parse arg msg
  115.     "INFO" msg
  116.     return
  117. /**************************************************************************/
  118. err: procedure expose global. sigl rc
  119. parse arg s,res
  120.     "CURRENT" 0
  121.     "TOTAL" 1
  122.     "ERR" s
  123.     exit
  124. /**************************************************************************/
  125. error:
  126.     call err "Error" sigl rc
  127.     exit
  128. syntax:
  129.     call err "Syntax" sigl rc
  130.     exit
  131. halt:
  132.     exit
  133. break_c:
  134.     call err global.str.INTERRUPTED
  135. /**************************************************************************/
  136. encodeB64: procedure
  137. parse arg s
  138.     s=c2b(s)
  139.     a=""
  140.     do while s~=""
  141.         parse var s c +6 s
  142.         a=a||substr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c2d(b2c(left(c"0000",6)))+1,1)
  143.     end
  144.     l=length(c)
  145.     if l<6 then a=a||copies("=",(6-l)/2)
  146.     return a
  147. /**************************************************************************/
  148. etime: procedure
  149. parse arg s
  150.     s=s%1
  151.     m=s%60
  152.     s=s//60
  153.     h=m%60
  154.     m=m//60
  155.     return right("00"h,2)":"right("00"m,2)":"right("00"s,2)
  156. /**************************************************************************/
  157. resolveHost: procedure expose global.
  158.     if global.proxy~="" then do
  159.         call info "Resolving host <"global.proxy">..."
  160.         global.sin.addrAddr=resolve(global.proxy)
  161.         if global.sin.addrAddr==-1 then call err "Host <"global.proxy">" global.str.HOSTNTFOUND".",1
  162.         global.sin.addrPort=global.proxyport
  163.         global.peer="<"global.proxy":"global.proxyport">"
  164.     end
  165.     else do
  166.         call info global.str.RESOLVINGHOST "<"global.host">..."
  167.         global.sin.addrAddr=resolve(global.host)
  168.         if global.sin.addrAddr==-1 then call err "Host <"global.host">" global.str.HOSTNTFOUND".",1
  169.         global.sin.addrPort=global.hostport
  170.         global.peer="<"global.host":"global.hostport">"
  171.     end
  172.     return
  173. /**************************************************************************/
  174. connectHost: procedure expose global.
  175.     global.sin.addrFamily="INET"
  176.     global.sock=socket("INET","STREAM")
  177.     if global.sock<0 then call err global.str.NOSOCKET
  178.     call info global.str.CONNECTING
  179.     if global.timeout>0 then do
  180.         global.tim=CreateTimer()
  181.         ts=TimerSignal(global.tim)
  182.         call SetSocketSignals(or(ts,2**12))
  183.         call StartTimer(global.tim,global.timeout)
  184.     end
  185.     if connect(global.sock,"GLOBAL.SIN")<0 then call err global.str.CANTCONNECT global.peer
  186.     return
  187. /**************************************************************************/
  188. sendrequest: procedure expose global.
  189.     call info global.str.SENDINGREQ
  190.     if global.range="" then httpV = "HTTP/1.1"
  191.     else httpV = "HTTP/1.0"
  192.     if left(global.remote,1)~="/"  then global.remote="/"global.remote
  193.     if global.proxy~="" then req="GET" global.url httpV
  194.     else req="GET" global.remote httpV
  195.     hst="Host:" global.host
  196.     usr="User-Agent: GetIt/4.0 (Amiga-ARexx-rxsocket.library)"
  197.     if global.login~="" then auth="Authorization: Basic" encodeB64(global.login":"global.pass)
  198.     else auth=""
  199.     if global.nocache then cache="Pragma: no-cache"
  200.     else cache=""
  201.  
  202.     req=req"D0A"x ||  usr"D0A"x
  203.     if auth~="" then req=req||auth"D0A"x
  204.     if cache~="" then req=req||cache"D0A"x
  205.     if hst~="" then req=req||hst"D0A"x
  206.     if global.range~="" then req=req||"Range: bytes="global.range"-" || "D0A"x
  207.     if global.ims~="" then req=req||"If-modified-since:" global.ims || "D0A"x
  208.     if global.referer~="" then req=req||"Referer:" global.referer || "D0A"x
  209.     req=req"D0A"x
  210.     if send(global.sock,req)~=length(req) then call err global.str.TXERR
  211.     return
  212. /**************************************************************************/
  213. receiveHead: procedure expose global.
  214.     call info global.str.HEADREC
  215.     len=recvline(global.sock,"BUF",256)
  216.     if len<0 then call err global.str.RXTIMEOUTERR
  217.     if len=0 then call err global.str.EMPTY
  218.     if left(buf,5)~="HTTP/" then call err global.str.BADANSWER
  219.     parse var buf "HTTP/"ver code coderest"D0A"x
  220.  
  221.     global.rhost=""
  222.     go=1
  223.     global.length=""
  224.     last=""
  225.     mime=""
  226.     hs="HTTP/"ver code coderest"A"x
  227.     do while go
  228.         len=RecvLine(global.sock,"BUF",256)
  229.         if len<0 then call err global.str.RXTIMEOUTERR
  230.         parse var buf b": "rest"D0A"x
  231.         select
  232.             when b=="Content-Length" then global.length=rest
  233.             when b=="Last-Modified" then last=rest
  234.             when b=="Content-type" then mime=rest
  235.             when b=="WWW-Authenticate" then global.reqauth=rest
  236.             when b=="Location" then global.location=rest
  237.             when b=="Host" then global.rhost=rest
  238.             otherwise nop
  239.         end
  240.         go=(buf~="D0A"x)
  241.         if go then do
  242.             hs=hs || b":" rest || "A"x
  243.         end
  244.     end
  245.     if global.timeout>0 then call FreeTimer(global.tim)
  246.  
  247.     if code=401 then do
  248.         "INFO" code coderest
  249.         "AUTH" global.reqauth
  250.         exit
  251.     end
  252.  
  253.     if code=301 | code=302 then do
  254.         "MOVED" global.location global.rhost
  255.         exit
  256.     end
  257.  
  258.     if code=304 then call err global.str.NOTMODIFIED global.ims
  259.     if global.range~="" then do
  260.         if code=200 then do
  261.             "ASKOVER"
  262.             if rc then do
  263.                 global.range=""
  264.             end
  265.             else call err global.str.EXITED
  266.         end
  267.         else if code~=206 then call err global.str.SERVERERR":" code coderest
  268.     end
  269.     else if code~=200 then call err global.str.SERVERERR":" code coderest
  270.     return
  271. /**************************************************************************/
  272. openFile: procedure expose global.
  273.     if global.range="" then res=Open("OUT",global.locale,"W")
  274.     else res=Open("OUT",global.locale,"A")
  275.     if ~res then call err "Can't open file",1
  276.     call SetComment(global.locale,global.u)
  277.     fs=statef(global.locale)
  278.     if fs~="" then parse var fs d global.size
  279.     return
  280. /**************************************************************************/
  281. parseLength: procedure expose global.
  282.     tempLen=global.length
  283.     if global.length~="" then do
  284.         if global.range~="" then do
  285.             global.length=global.length+global.range
  286.             if global.range>=global.size then call err "Range:"global.range" >= Length:"global.size
  287.         end
  288.         global.l=global.l || global.length
  289.     end
  290.     else global.l=global.l || global.str.UNKONW
  291.     if global.length~="" then "TOTAL" global.length
  292.     return
  293. /**************************************************************************/
  294. getStrings: procedure expose global.
  295.     catalog=OpenCatalog(global.cat,"english",0)
  296.     global.str.STALLED=GetCatalogStr(catalog,1100,"stalled at")
  297.     global.str.BYTES=GetCatalogStr(catalog,1101,"bytes")
  298.     global.str.RXERR=GetCatalogStr(catalog,1102,"Error receiving")
  299.     global.str.SIZE=GetCatalogStr(catalog,1103,"Size")
  300.     global.str.UNKNOWSIZE=GetCatalogStr(catalog,1104,"unknown size")
  301.     global.str.AT=GetCatalogStr(catalog,1105,"at")
  302.     global.str.ASPECTED=GetCatalogStr(catalog,1106,"Aspcted")
  303.     global.str.RECEIVED=GetCatalogStr(catalog,1107,"received")
  304.     global.str.INTERRUPTED=GetCatalogStr(catalog,1108,"Transfer interrupted")
  305.     global.str.RESOLVINGHOST=GetCatalogStr(catalog,1109,"Resolving host")
  306.     global.str.HOSTNTFOUND=GetCatalogStr(catalog,1110,"Host not found")
  307.     global.str.NOSOCKET=GetCatalogStr(catalog,1111,"No socket")
  308.     global.str.CONNECTING=GetCatalogStr(catalog,1112,"Connecting...")
  309.     global.str.CANTCONNECT=GetCatalogStr(catalog,1113,"Can't connect to")
  310.     global.str.SENDINGREQ=GetCatalogStr(catalog,1114,"Sending request...")
  311.     global.str.TXERR=GetCatalogStr(catalog,1115,"Error sending")
  312.     global.str.HEADREC=GetCatalogStr(catalog,1116,"Receiving and parsing head...")
  313.     global.str.RXTIMEOUTERR=GetCatalogStr(catalog,1117,"Error receiving or timeout")
  314.     global.str.EMPTY=GetCatalogStr(catalog,1118,"The requested document contains no data")
  315.     global.str.BADANSWER=GetCatalogStr(catalog,1119,"Bad answer from server")
  316.     global.str.NOTMODIFIED=GetCatalogStr(catalog,1120,"File was not modified since")
  317.     global.str.SERVERERR=GetCatalogStr(catalog,1121,"Server error")
  318.     global.str.UNKONW=GetCatalogStr(catalog,1122,"unknown")
  319.     global.str.EXITED=GetCatalogStr(catalog,1123,"Transfer terminated")
  320.     call CloseCatalog(catalog);
  321.     return
  322. /**************************************************************************/
  323. /*$VER: GetItFun.rexx 4.5 (17.5.99)*/
  324.